//
//  UIScrollView+RCKRefreshControl.m
//  Refresh
//
//  Created by weitayinqing on 15/9/28.
//  Copyright © 2015年 苏昌雄. All rights reserved.
//

#import "UIScrollView+RCKRefreshControl.h"
#import<objc/runtime.h>
#import "RickyRefreshView.h"
#import "RickyRefreshNormal.h"
#import "RickyRefreshCoreTextView.h"
#import "RickyRefreshGifView.h"

#define UIViewAnimation(time) \
[UIView animateWithDuration:time animations:^

@interface UIScrollView ()
@property(nonatomic,strong)RickyRefreshView *header;
@property(nonatomic,strong)RickyRefreshView *footer;
@end

@implementation UIScrollView (RCKRefreshControl)

#pragma mark --setter

- (RickyRefreshView *)header
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setHeader:(RickyRefreshView *)header
{
    objc_setAssociatedObject(self, @selector(header),header,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setFooter:(RickyRefreshView *)footer
{
    objc_setAssociatedObject(self, @selector(footer), footer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark --
#pragma mark --public
/***************************************NormalHeader*****************************/
- (void)addNormalRickyRefreshHeaderViewWithCallBackAction:(void (^)())action
{
    if (!self.header) {
        RickyRefreshNormal *header = [[RickyRefreshNormal alloc] initWithRefreshViewType:RickyRefreshViewTypeHeader];
        header.actionBlock = [action copy];
        [self addSubview:header];
        self.header = header;
    }

}

/***************************************NormalFooter*****************************/
- (void)addNormalRickyRefreshFooterViewWithCallBackAction:(void (^)())action
{
    if (!self.footer) {
        RickyRefreshNormal *footer = [[RickyRefreshNormal alloc] initWithRefreshViewType:RickyRefreshViewTypeFooter];
        footer.actionBlock = [action copy];
        [self addSubview:footer];
        self.footer = footer;
    }
}
/***************************************TextHeader*****************************/
- (void)addPullTextRickyRefreshHeaderViewWithPullText:(NSString *)pullText pullTextColor:(UIColor *)pullTextColor pullTextFont:(UIFont *)pullTextFont refreshingText:(NSString *)refreshingText refreshingTextColor:(UIColor *)refreshingTextColor refreshingTextFont:(UIFont *)refreshingTextFont withCallBackAction:(void (^)())action
{
    if (!self.header) {
        RickyRefreshCoreTextView *header = [[RickyRefreshCoreTextView alloc] initWithRefreshViewType:RickyRefreshViewTypeHeader];
        header.pullText = pullText.length ==0?RickyHeaderNormalTitle:pullText;
        header.pullTextColor = pullTextColor?pullTextColor:[UIColor blackColor];
        header.pullTextFont = pullTextFont?pullTextFont:[UIFont systemFontOfSize:20];
        
        header.refreshingText = refreshingText.length==0?RickyHeaderRefreshingTitle:refreshingText;
        header.refreshingTextColor = refreshingTextColor?refreshingTextColor:[UIColor blackColor];
        header.refreshingTextFont = refreshingTextFont?refreshingTextFont:[UIFont systemFontOfSize:20];
        header.actionBlock = [action copy];
        [self addSubview:header];
        self.header =header;
        
    }
}
/***************************************TextFooter*****************************/
- (void)addPullTextRickyRefreshFooterViewWithPullText:(NSString *)pullText pullTextColor:(UIColor *)pullTextColor pullTextFont:(UIFont *)pullTextFont refreshingText:(NSString *)refreshingText refreshingTextColor:(UIColor *)refreshingTextColor refreshingTextFont:(UIFont *)refreshingTextFont withCallBackAction:(void (^)())action
{
    if (!self.footer) {
        RickyRefreshCoreTextView *footer = [[RickyRefreshCoreTextView alloc] initWithRefreshViewType:RickyRefreshViewTypeHeader];
        footer.pullText = pullText.length ==0?RickyHeaderNormalTitle:pullText;
        footer.pullTextColor = pullTextColor?pullTextColor:[UIColor blackColor];
        footer.pullTextFont = pullTextFont?pullTextFont:[UIFont systemFontOfSize:20];
        
        footer.refreshingText = refreshingText.length==0?RickyHeaderRefreshingTitle:refreshingText;
        footer.refreshingTextColor = refreshingTextColor?refreshingTextColor:[UIColor blackColor];
        footer.refreshingTextFont = refreshingTextFont?refreshingTextFont:[UIFont systemFontOfSize:20];
        footer.actionBlock = [action copy];
        [self addSubview:footer];
        self.header =footer;
        
    }

    
}

/***************************************gifHeader*****************************/
 -(void)addGifRickyRefreshHeaderViewWithPlaceHolderImage:(UIImage *)image animationImageArray:(NSArray *)animationImage title:(NSString *)title withCallBackAction:(void (^)())action
{
    if (!self.header) {
        RickyRefreshGifView *header = [[RickyRefreshGifView alloc] initWithRefreshViewType:RickyRefreshViewTypeHeader];
        header.placeHolderImage = image;
        header.animationImages = animationImage;
        header.title = title;
        header.actionBlock= [action copy];
        [self addSubview:header];
        self.header = header;
    }
}

- (void)completeRickyRefreshHeaderWithCompleteType:(RickyRefreshCompleteType)completeType
{
    if (self.header) {
        [self.header setCompleteType:completeType];
    }
}

- (void)completeRickyRefreshFooterWithCompleteType:(RickyRefreshCompleteType)completeType
{
    if (self.footer) {
        [self.footer setCompleteType:completeType];
    }

}

- (void)setHeaderRefreshAtBegin:(BOOL)begin
{
    if (self.header) {
        if (begin) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.header setRefreshState:RickyRefreshStateRefreshing withCallBack:NULL];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.header setRefreshState:RickyRefreshStateRefreshing withCallBack:NULL];
            });
        }
    }
}

#pragma mark--
#pragma mark ---setter
- (void)setRefreshEndbled:(BOOL)refreshEnadled
{
    [self setHeaderRefreshEndbled:refreshEnadled];
    [self setFooterRefreshEndbled:refreshEnadled];
}

- (void)setHeaderRefreshEndbled:(BOOL)enadled
{
    if (enadled) {
        self.header.hidden = NO;
    }else{
        [self.header setRefreshState:RickyRefreshStateNormal withCallBack:NULL];
        self.header.hidden= YES;
        
    }
}

- (void)setFooterRefreshEndbled:(BOOL)enadled
{
    if (enadled) {
        self.footer.hidden = NO;
    }else{
        [self.footer setRefreshState:RickyRefreshStateNormal withCallBack:NULL];
        self.header.hidden= YES;
        
    }

}
@end
